package com.eerl.pdev.gen;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import com.intellij.util.ui.JBUI;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.Map;

public class MyToolWindowFactory implements ToolWindowFactory {

    private static final JTextField modNameTextField = new JTextField(15);
    private static final JTextField authorTextField = new JTextField(15);
    private static final JTextField descTextField = new JTextField(20);
    private ToolWindow toolWindow;

    public static final String VAR_KEY_MOD = "mod";
    public static final String VAR_KEY_AUTHOR = "author";
    public static final String VAR_KEY_DESC = "desc";
    public MyToolWindowFactory() {}

    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        ContentFactory contentFactory = ContentFactory.getInstance();
        Content content = contentFactory.createContent(createPanel(), "", false);
        toolWindow.getContentManager().addContent(content);
        this.toolWindow = toolWindow;
        toolWindow.hide();
    }

    private static AnActionEvent event;
    public static void setAnEvent(AnActionEvent e) {
        event = e;
    }

    private JComponent createPanel() {

        JPanel panel = new JPanel();
        BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
        panel.setLayout(boxLayout);

        JLabel modNameLabel = new JLabel("mod   ");
        JLabel authorLabel = new JLabel("author");
        JLabel descLabel = new JLabel("desc  ");
        // JLabel protoLabel 以后再看吧

        JLabel[] jLabels = {modNameLabel, authorLabel, descLabel};
        // 拆东墙补西墙
        JTextField[] inputs = {modNameTextField, authorTextField, descTextField};

        for (int i = 0; i < jLabels.length; i++) {
            jLabels[i].setFont(JBUI.Fonts.create("Microsoft YaHei, JetBrains Mono", 14));
            JPanel innerJPanel = new JPanel();
            innerJPanel.setMaximumSize(new Dimension(1000, 50));
            FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
            innerJPanel.setLayout(layout);
            innerJPanel.add(jLabels[i]);
            innerJPanel.add(inputs[i]);
            panel.add(innerJPanel);
        }

        JButton OKButton = new JButton("GenTemplate");
        OKButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e == null) return;
                Map<String, String> vars = new HashMap<>();
                vars.put(VAR_KEY_MOD, modNameTextField.getText());
                vars.put(VAR_KEY_DESC, descTextField.getText());
                vars.put(VAR_KEY_AUTHOR, authorTextField.getText());
                GenEmptyTemplate.gen(vars, event);
                toolWindow.hide();

            }
        });

        panel.add(OKButton);
        return panel;
    }
}